using System;
using System.Configuration;
using System.Data;
using System.Collections;
using System.Data.Common;
using System.Data.OracleClient;

namespace App_Code.PublicHelper
{
    public static class DataProductHelper
    {
        private static  int HitType = 3;
        public static int GetPageCount()
        {
            string sql = "select count(*) from dataproduct t";
            int totalCount = int.Parse(DatabaseHelper.ExecuteScalar(sql).ToString());
            int pageCount = totalCount % MyConstants.CountInPage != 0 ? totalCount / MyConstants.CountInPage + 1 : totalCount / MyConstants.CountInPage;
            return pageCount;
        }

        public static DataSet QueryByID(int id)
        {
            string sql = string.Format("select * from dataproduct t where id={0}", id);
            return DatabaseHelper.ExecuteDataset(sql);
        }

        public static DataSet QueryByPage(int page)
        {
            int begin = (page - 1) * MyConstants.CountInPage;
            int end = begin + MyConstants.CountInPage;

            string sql = string.Format("SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM dataproduct t LEFT JOIN hits t2 ON t.id = t2.pid AND t2.ptype = {0} order by hit desc NULLS LAST) A WHERE ROWNUM <= {1})WHERE RN > {2}", HitType, end, begin);
            return DatabaseHelper.ExecuteDataset(sql);
        }

        public static DataSet QueryTopN(int n)
        {
            string sql = string.Format("SELECT * FROM (SELECT * FROM dataproduct t LEFT JOIN hits t2 ON t.id = t2.pid AND t2.ptype = {0} order by hit desc NULLS LAST) WHERE ROWNUM <= {1} ORDER BY ROWNUM ASC", HitType, n);
            return DatabaseHelper.ExecuteDataset(sql);
        }

        public static void UpdateHits(int pid)
        {
            HitHelper.UpdateHits(pid, HitType);
        }
    }
}

